﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Garlic.CC.WebUtility;
using Garlic.BLL;
using Garlic.DM.DataModel;
using Garlic.CC.BaseClass;
using Garlic.BLL.Common;

public partial class SystemManagement_RoleEdit : BasePage
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string roleID = Request.QueryString["RoleID"];

            if (!(string.IsNullOrEmpty(roleID)))
            {
                
                RoleInfo role = RoleManager.GetInfo(int.Parse(roleID));
                
                hdRoleID.Value = role.Role_id.ToString();
                RoleNameTextBox.Text = role.Role_name;
                SetRoleTypeSelect(role.Role_type);
                CommentsTextBox.Text = role.Comments;
            }
        }
    }

    private void SetRoleTypeSelect(int roleType)
    {
        switch (roleType)
        {
            case 0:
                RoleTypeDropDownList.SelectedValue = "0";
                break;
            //default:
            //    throw new Exception();
        }
    }

    protected void TopToolBar_OnClick(object sender, Garlic.UI.Components.IconButtonClickEventArgs args)
    {
        try
        {
            RoleInfo role;


            if (string.IsNullOrEmpty(hdRoleID.Value))
            {
                RoleInfo r = RoleManager.GetRoleByRoleName(RoleNameTextBox.Text);
                if (r != null)
                {
                    this.Message = "角色名重复，请确认。";
                    return;
                }
                role = new RoleInfo();
                role.Create_user = CurrentUser.UserID;
                role.Create_time = DateTime.Now;
            }
            else
            {
                role = RoleManager.GetInfo(int.Parse(hdRoleID.Value));
                role.Modify_time = DateTime.Now;
                role.Modify_user = CurrentUser.UserID;
            }

            role.Role_name = RoleNameTextBox.Text;
            role.Role_type = int.Parse(RoleTypeDropDownList.SelectedValue);
            role.Comments = CommentsTextBox.Text;

            if (string.IsNullOrEmpty(hdRoleID.Value))
            {
                role.Role_id = SequenceManager.GetNextVal("RoleID");
                RoleManager.Add(role);
            }
            else
            {
                RoleManager.Update(role);
            }
            this.CloseVirtualWindow(true);
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }

    }

    private void ShowMessage(string msg)
    {
        this.Message = msg;
    }
}
